
// ---------------------------------------------------
// | File Name   : relay.c
// | Author   	 : chagn7z
// | Create Date : 2014-6-16
// ---------------------------------------------------

#include "stm32f10x.h"
#include "user_cfg.h"
#include "relay.h"
#include "include.h"

u8 relayb_time[2][2] = {{30, 8}, {0, 16}};
u8 relayc_time[2][2] = {{0, 10}, {0, 14}};
u8 relayd_time[8][2] = {{0, 1}, {0, 3}, {0, 5}, {0, 7}, {0, 9}, {0, 10}, {0, 12}, {0, 14}};
u8 relaye_time[2][2] = {{0, 0}, {30, 0}};
/* all relay working status, 1 for open, 0 for close.*/
u8 relay_status = 0;		//| - | - | E | D | C | B2 | B1 | A |

void relay_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE);

	/* GPIO_A 8-12 Pin for relay 3-7 */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12);

	/* GPIO_C 8-9 Pin for relay 1-2 */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	GPIO_ResetBits(GPIOC, GPIO_Pin_8 | GPIO_Pin_9);

}

void relay_dt(void)
{
	relay_dt_a();
	
	relay_dt_b();
	
	relay_dt_c();
	
	relay_dt_d();
	
	relay_dt_e();
	
	relay_dt_status();
}

/* detect relay a working status */
void relay_dt_a(void)
{
	u8 hour_on, hour_off;
	u8 min_on, min_off;
	u8 hour_cur, min_cur;
	u8 relaya_status;

	relaya_status = 0;

	hour_cur = work_info.time[2];
	min_cur  = work_info.time[1];
	hour_on  = work_info.relaya_on[1];
	hour_off = work_info.relaya_off[1];
	min_on   = work_info.relaya_on[0];
	min_off  = work_info.relaya_off[0];

	if (hour_on < hour_off)
	{
		if ((hour_cur > hour_on) && (hour_cur < hour_off))
		{
			relaya_status = 1;
		}
		else if (hour_cur == hour_on)
		{
			if (min_cur >= min_on)
			{
				relaya_status = 1;
			}
		}
		else if (hour_cur == hour_off)
		{
			if (min_cur < min_off)
				relaya_status = 1;
		}
	}
	if (hour_on > hour_off)
	{
		if ((hour_cur > hour_on) || hour_cur < hour_off)
		{
			relaya_status = 1;
		}
		else if (hour_cur == hour_on)
		{
			if (min_cur >= min_on)
			{
				relaya_status = 1;
			}
		}
		else if (hour_cur == hour_off)
		{
			if (min_cur < min_off)
				relaya_status = 1;
		}
	}

	if (relaya_status)
		relay_status |= RELAYA_BIT;
	else
		relay_status &= ~RELAYA_BIT;
}

/* detect relay b working status */
void relay_dt_b(void)
{
	u8 i,j,k;
	u8 hour_on, hour_off;
	u8 min_on, min_off;
	u8 hour_cur, min_cur, sec_cur;
	u8 relayb_status[2][3];
	u8 mode;
	u8 work;

	for (j=0; j<2; j++)
		for (i=0; i<3; i++)
			relayb_status[j][i] = 0;

	hour_cur = work_info.time[2];
	min_cur  = work_info.time[1];
	sec_cur  = work_info.time[0];

	for (k=0; k<2; k++)
	{		
		work     = work_info.relayb_date[k];
		mode     = work_info.relayb_mode[k];

		if ((mode == 1) || (mode == 2))
		{
			work = 1;
		}
		else if ((mode == 3) || (mode == 4) || (mode == 5))
		{
			if ((hour_cur == 23) && (min_cur == 59) && (sec_cur == 59))
			{
				work++;

				if (mode == 3)
					if (work >= 3)
						work = 1;

				if (mode == 4)
					if (work >= 4)
						work = 1;

				if (mode == 5)
					if (work >= 5)
						work = 1;
			}
		}
		else if (mode == 6)
		{
			work = 0;
		}

		/* store date to global struct */
		work_info.relayb_date[k] = work;

		if (work == 1)
		{	
			if (mode == 2)
				j = 2;
			else 
				j = 1;

			for (i=0; i<j; i++)
			{
				hour_on  = relayc_time[i][1];
				hour_off = hour_on;
				min_on   = relayc_time[i][0];
				min_off  = min_on + 20;

				if (min_off >= 60)
				{
					hour_off ++;
					min_off  -= 60;

					if (hour_off >= 24)
						hour_off -= 24;
				}

				/* judege relay working status */
				if (hour_on < hour_off)
				{
					if ((hour_cur > hour_on) && (hour_cur < hour_off))
					{
						relayb_status[k][i] = 1;
					}
					else if (hour_cur == hour_on)
					{
						if (min_cur >= min_on)
						{
							relayb_status[k][i] = 1;
						}
					}
					else if (hour_cur == hour_off)
					{
						if (min_cur < min_off)
							relayb_status[k][i] = 1;
					}
				}
				if (hour_on > hour_off)
				{
					if ((hour_cur > hour_on) || hour_cur < hour_off)
					{
						relayb_status[k][i] = 1;
					}
					else if (hour_cur == hour_on)
					{
						if (min_cur >= min_on)
						{
							relayb_status[k][i] = 1;
						}
					}
					else if (hour_cur == hour_off)
					{
						if (min_cur < min_off)
							relayb_status[k][i] = 1;
					}
				}
			}
		}
		else
		{
			relayb_status[k][i] = 0;
		}
	}

	for (i=0; i<2; i++)
		if (mode == 2)
			if ((relayb_status[i][0]) || relayb_status[i][1])
				relayb_status[i][2] = 1;
		else if (mode == 6)
			relayb_status[i][2] = 0;
		else
			if (relayb_status[i][0])
				relayb_status[i][2] = 1;

	if (relayb_status[0][2])
		relay_status |= RELAYB1_BIT;
	else 
		relay_status &= ~RELAYB1_BIT;

	if (relayb_status[1][2])
		relay_status |= RELAYB2_BIT;
	else 
		relay_status &= ~RELAYB2_BIT;	
}

/* detect relay c working status */
void relay_dt_c(void)
{
	u8 i;
	u8 hour_on, hour_off;
	u8 min_on, min_off;
	u8 hour_cur, min_cur;
	u8 relayc_status[3];

	for (i=0; i<3; i++)
		relayc_status[i] = 0;

	hour_cur = work_info.time[2];
	min_cur  = work_info.time[1];

	for (i=0; i<2; i++)
	{	
		hour_on  = relayc_time[i][1];
		hour_off = hour_on + 2;
		min_on   = relayc_time[i][0];
		min_off  = min_on;

		if (min_off >= 60)
		{
			hour_off ++;
			min_off  -= 60;

			if (hour_off >= 24)
				hour_off -= 24;
		}

		/* judege relay working status */
		if (hour_on < hour_off)
		{
			if ((hour_cur > hour_on) && (hour_cur < hour_off))
			{
				relayc_status[i] = 1;
			}
			else if (hour_cur == hour_on)
			{
				if (min_cur >= min_on)
				{
					relayc_status[i] = 1;
				}
			}
			else if (hour_cur == hour_off)
			{
				if (min_cur < min_off)
					relayc_status[i] = 1;
			}
		}
		if (hour_on > hour_off)
		{
			if ((hour_cur > hour_on) || hour_cur < hour_off)
			{
				relayc_status[i] = 1;
			}
			else if (hour_cur == hour_on)
			{
				if (min_cur >= min_on)
				{
					relayc_status[i] = 1;
				}
			}
			else if (hour_cur == hour_off)
			{
				if (min_cur < min_off)
					relayc_status[i] = 1;
			}
		}
	}

	for (i=0; i<3; i++)
		if (relayc_status[i])
			relayc_status[2] = 1;

	if (relayc_status[2])
		relay_status |= RELAYC_BIT;
	else 
		relay_status &= ~RELAYC_BIT;
}

/* detect relay d working status */
void relay_dt_d(void)
{
	u8 i;
	u8 hour_on, hour_off;
	u8 min_on, min_off;
	u8 hour_cur, min_cur;
	u8 relayd_status[9];

	for (i=0; i<9; i++)
		relayd_status[i] = 0;

	hour_cur = work_info.time[2];
	min_cur  = work_info.time[1];

	for (i=0; i<8; i++)
	{	
		hour_on  = relayd_time[i][1];
		hour_off = hour_on + 1;
		min_on   = relayd_time[i][0];
		min_off  = min_on;

		if (min_off >= 60)
		{
			hour_off ++;
			min_off  -= 60;

			if (hour_off >= 24)
				hour_off -= 24;
		}

		/* judege relay working status */
		if (hour_on < hour_off)
		{
			if ((hour_cur > hour_on) && (hour_cur < hour_off))
			{
				relayd_status[i] = 1;
			}
			else if (hour_cur == hour_on)
			{
				if (min_cur >= min_on)
				{
					relayd_status[i] = 1;
				}
			}
			else if (hour_cur == hour_off)
			{
				if (min_cur < min_off)
					relayd_status[i] = 1;
			}
		}
		if (hour_on > hour_off)
		{
			if ((hour_cur > hour_on) || hour_cur < hour_off)
			{
				relayd_status[i] = 1;
			}
			else if (hour_cur == hour_on)
			{
				if (min_cur >= min_on)
				{
					relayd_status[i] = 1;
				}
			}
			else if (hour_cur == hour_off)
			{
				if (min_cur < min_off)
					relayd_status[i] = 1;
			}
		}
	}

	for (i=0; i<8; i++)
		if (relayd_status[i])
			relayd_status[8] = 1;

	if (relayd_status[8])
		relay_status |= RELAYD_BIT;
	else 
		relay_status &= ~RELAYD_BIT;
}

/* detect relay e working status */
void relay_dt_e(void)
{
	u8 i;
	u8 st_a;
	u8 hour_on, hour_off;
	u8 min_on, min_off;
	u8 hour_cur, min_cur;
	u8 relaye_status[3];

	relaye_status[2] = 0;
	relaye_status[1] = 0;
	relaye_status[0] = 0;

	hour_cur = work_info.time[2];
	min_cur  = work_info.time[1];

	st_a = (relay_status & RELAYA_BIT) ? 1 : 0;

	if (st_a)
	{
		relay_status |= RELAYE_BIT;
	}
	else
	{
		for (i=0; i<2; i++)
		{	
			min_on   = relaye_time[i][0];
			min_off  = min_on + 15;

			/* judege relay working status */
			if ((min_cur >= min_on) && (min_cur < min_off))
				relaye_status[i] = 1;
			else 
				relaye_status[i] = 0;			
		}

		for (i=0; i<2; i++)
			if (relaye_status[i])
				relaye_status[2] = 1;

		if (relaye_status[2])
			relay_status |= RELAYE_BIT;
		else 
			relay_status &= ~RELAYE_BIT;
	}
}

void relay_dt_status(void)
{
	RELAY_A1 = relay_status & RELAYA_BIT  ? 1 : 0;
	RELAY_A2 = relay_status & RELAYA_BIT  ? 1 : 0;
	RELAY_B1 = relay_status & RELAYB1_BIT ? 1 : 0;
	RELAY_B2 = relay_status & RELAYB2_BIT ? 1 : 0;
	RELAY_C  = relay_status & RELAYC_BIT  ? 1 : 0;
	RELAY_D  = relay_status & RELAYD_BIT  ? 1 : 0;
	RELAY_E  = relay_status & RELAYE_BIT  ? 1 : 0;
}





